<template>
	<view class="Info">
		<form @submit="form_submit">
			<view class="InfoList">
				<view class="InfoList-item">
					<view class="">头像</view>
					<button hover-class="none" open-type="chooseAvatar" @chooseavatar="choose_avatar_event"
						@tap="choose_avatar_event">
						<!-- <image :src="avatarUrl" mode="widthFix" class="tx"></image> -->
						<image :src="(user_data.avatar || default_avatar)" mode="widthFix" class="tx"></image>
					</button>
				</view>
				<view class="InfoList-item">
					<view class="">呢称</view>
					<input :type="(application_client_type == 'weixin') ? 'nickname' : 'text'" name="nickname"
						:value="user_data.nickname || ''" maxlength="16" placeholder-class="cr-grey" class="cr-base"
						placeholder="呢称2~16个字符">
				</view>
				<view class="InfoList-item">
					<view class="">手机号码</view>
					<view class="phone">{{user_data.phone}}</view>
				</view>
				<!-- <view class="InfoList-item">
					<view class="">性别</view>
					<view class="dyh">
						<picker name="gender" :value="user_data.gender || ''" data-field="gender" @change="select_change_event">
							<view :class="'picker '+((user_data.gender || null) == null ? 'cr-grey' : '')">
								{{user_data.gender || '请选择性别'}}
							</view>
						</picker>
						<image
							src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/d0b60d4c410647ae8ede7a9a1ec7447a.png"
							mode="widthFix" class="dayuhao"></image>
					</view>
				</view> -->
				<view class="InfoList-item">
					<view class="">生日</view>
					<view class="dyh">
						<picker name="birthday" mode="date" :value="user_data.birthday || ''" data-field="birthday"
							@change="select_change_event">
							<view :class="'picker '+((user_data.birthday || null) == null ? 'cr-grey' : '')">
								{{user_data.birthday || '请选择生日'}}
							</view>
						</picker>
						<image
							src="https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/d0b60d4c410647ae8ede7a9a1ec7447a.png"
							mode="widthFix" class="dayuhao"></image>
					</view>
				</view>
			</view>
			<button type="default" form-type="submit" hover-class="none">保存</button>
		</form>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				application_client_type: app.globalData.application_client_type(),
				default_avatar: 'https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/151ce7481d8f45279557f5b51e78c595.jpg',
				address: "",
				gsName: "爱曦珠宝",
				birthday: "",
				user_data: {},
				entryTime: 0,
			}
		},
		// 分享给朋友
		onShareAppMessage(res) {
			if (res.from === 'button') {
				console.log(res.target, "target");
			}
			return {
				title: app.globalData.data.tenantName,
				path: "pages/user_setInfo/setInfo?numberId=" + app.globalData.data.numberId,
				mpId: app.globalData.data.appid
			}
		},
		// 分享朋友圈
		// onShareTimeline(res) {
		// 	return{
		// 		title:app.globalData.data.tenantName,
		// 		type:0,
		// 		summary:""
		// 	}
		// },
		onShow() {
			// 记录用户进来时间
			this.entryTime = Date.now();

		},
		onUnload() {
			this.OnEntryTime();
		},
		onLoad(params) {
			if (typeof(params) != 'undefined') {
				var numberId = params.numberId || '';
				if (numberId != '') {
					uni.clearStorageSync();
					app.globalData.data.numberId = numberId;
				}
			}
			this.init();
		},
		methods: {
			init(e) {
				var user = app.globalData.get_user_info(this, "init", "");
				if (user != false) {
					// 用户未绑定用户则转到登录页面
					if (app.globalData.user_is_need_login(user)) {
						uni.navigateTo({
							url: "/pages/login/login?event_callback=init"
						})
					} else {
						this.get_data();
					}
				}
			},
			OnEntryTime() {
				var radarlog_data = {
					pageUrl: "pages/user_setInfo/setInfo",
					name: "修改个人信息页",
					remark: "修改个人信息页停留时间",
					entryTime: this.entryTime,
				}
				uni.request({
					url: app.globalData.get_request_url("radarlog/add"),
					data: radarlog_data,
					method: "POST",
					success: (res) => {
						console.log(res, "修改个人信息页停留时间");
					}
				})
			},
			get_data() {
				var data = {}
				uni.request({
					url: app.globalData.get_request_url("personal/index"),
					data: data,
					method: "POST",
					success: (res) => {
						if (res.data.code == 200) {
							let tem = res.data.data;
							tem['birthday'] = tem.birthdays;
							this.user_data = tem;
						}
					}
				})
			},
			// 生日、性别选择事件
			select_change_event(e) {
				var temp = this.user_data;
				temp[e.currentTarget.dataset.field] = e.detail.value;
				this.user_data = temp;
			},
			// 性别
			// gender_envent(e) {
			// 	var index = e.detail.value || 0;
			// 	if (index >= 0) {
			// 		var data = this.gender_list[index];
			// 		this.gender_value = index;
			// 		this.user_data.gender = this.gender_list[this.gender_value];
			// 	}
			// },
			// 头像事件
			choose_avatar_event(e) {
				if (this.application_client_type == 'weixin') {
					this.upload_handle(e.detail.avatarUrl);
				} else {
					uni.chooseImage({
						count: 1,
						success(res) {
							if (res.tempFilePaths.length > 0) {
								this.upload_handle(res.tempFilePaths[0]);
							}
						}
					});
				}
			},
			// 上传处理
			upload_handle(image) {
				var self = this;
				let url = app.globalData.get_request_url("useravatarupload");
				uni.uploadFile({
					url: url,
					filePath: image,
					name: 'file',
					formData: {},
					success: function(res) {
						if (res.statusCode == 200) {
							var data = typeof res.data == 'object' ? res.data : JSON.parse(res.data);
							if (data.code == 200) {
								var temp = self.user_data;
								temp.avatar = data.data.data
								self.user_data = temp
							} else {
								uni.showToast({
									title: data.msg
								})
							}
						}
					}
				})
			},

			form_submit(e) {
				var form_data = {
					...e.detail.value,
					avatar: this.user_data.avatar || '',
				};
				uni.showLoading({
					title: '处理中...'
				});
				if(e.detail.value.birthday == ''){
					uni.showToast({
						title:"生日必选哦!"
					})
					return;
				}
				uni.request({
					url: app.globalData.get_request_url("personal/save"),
					data: form_data,
					method: "POST",
					success: (res) => {
						if (res.data.code == 200) {
							uni.hideLoading();
							var avatar = res.data.avatar
							uni.showToast({
								title: "保存成功"
							})
							uni.switchTab({
								url: "/pages/user/user"
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.InfoList {
		margin-top: 15px;
	}

	.Info button {
		width: 180px;
		height: 40px;
		text-align: center;
		line-height: 40px;
		background-color: #f7dcad;
		margin-top: 50px;
	}

	.InfoList-item {
		height: 50px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-left: 20px;
		padding-right: 20px;
		padding-bottom: 10px;
		border-bottom: 1px solid #f0f0f0;
	}

	.InfoList-item button {
		width: 50px;
		height: 50px;
		margin-top: 0;
		background-color: white;
		border: 1px solid white;
		margin-right: 10px;

		image {
			width: 50px;
			margin-left: -15px;
		}
	}

	.InfoList-item input {
		width: 100px;
		// border-bottom: 1px solid gray;
		text-align: center;
	}

	.InfoList-item image {
		width: 50px;
		border-radius: 10px;
	}

	.phone {
		color: gray;
	}

	.dyh {
		display: flex;
		align-items: center;
		color: gray;
		font-size: 12px;
	}

	.dyh image {
		width: 20px;
		margin-left: 5px;
	}
</style>